<?php
/**
 * Created by PhpStorm.
 * User: lyw
 * Date: 2016/5/24
 * Time: 14:26
 */

class User_promotion_codeController extends BaseController{
    public function index(){
        $userPromotion = new Do用户推广表();
        $user = new Do用户();
        $userName = $this->request('name');
        $where = sprintf("b.`公众号ID` = '%s'", G_SERV_ID);
        if($userName) {
            $where .= " and name like'%".$userName."%' ";
        }
        $count = $userPromotion->fields("count(*)")
            ->leftJoin($user,'b','a.用户id = b.id')
            ->where($where)
            ->getOneField();

        $userPromotion = new Do用户推广表();
        $page = new \tools\Page($count,15);
        $infos = $userPromotion->fields("a.*,b.name,b.telephone")
            ->offset($page->limit)
            ->limit($page->pagesize)
            ->leftJoin($user,'b','a.用户id = b.id')
            ->where($where)
            ->rows();
        foreach($infos as $k=>$v)
        {
            $userPromotionDetail = new Do用户推广详细();
            $num = $userPromotionDetail->fields('count(*)')->leftJoin($user,'b','a.uid = b.id')
                ->where('a.二维码编号="'.$v['二维码编号'].'" and b.认证>1')->getOneField();
            $infos[$k]['authentication'] = $num;
        }
        require $this->template;
    }

    public function show_user_detail(){
        $userPromotionDetail = new Do用户推广详细();
        $user = new Do用户();
        $code = $this->request('code','',true);
        $stime = $this->request('stime');
        $etime = $this->request('etime');
        $where = "二维码编号='".$code."'";
        if($stime)
        {
            $where.=" and 最初关注时间>='".$stime." 00:00:00'";
        }
        if($etime)
        {
            $where.=" and 最初关注时间<='".$etime." 23:59:59'";

        }
        $count = $userPromotionDetail->where($where)->count();
        $page = new \tools\Page($count,15);
        $infos = $userPromotionDetail->fields('b.*')->leftJoin($user,'b','a.uid = b.id')->offset($page->limit)
            ->limit($page->pagesize)
            ->where($where)->rows();
        require $this->template;
    }
}